package com.micai.springboot.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.micai.springboot.dto.RoleDto;
import com.micai.springboot.entity.RoleEntity;
import com.micai.springboot.mapper.RoleMapper;
import com.micai.springboot.service.RoleService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 * <p>
 * Author: 赵新国
 * Date: 2017/10/24 18:36
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    private static final Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Page<RoleDto> getRoleByPage(int pageNum, int pageSize, String searchParam, String startDate, String endDate) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> columnMap = new HashMap<String, Object>(2);
        if (!StringUtils.isBlank(searchParam)) {
            columnMap.put("name", searchParam);
        }
        if (!StringUtils.isBlank(startDate)) {
            columnMap.put("startDate", startDate);
        }
        if (!StringUtils.isBlank(endDate)) {
            columnMap.put("endDate", endDate);
        }
        Page<RoleEntity> roleEntities = roleMapper.findRoleByPage(columnMap);
        if (CollectionUtils.isEmpty(roleEntities)) {
            return null;
        }
        Page<RoleDto> roleDtos = new Page<RoleDto>();
        for (RoleEntity roleEntity : roleEntities) {
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(roleEntity, roleDto);
            roleDtos.add(roleDto);
        }
        return toDto(roleEntities, roleDtos);
    }

    @Override
    public Integer save(RoleDto roleDto) {
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(roleDto, roleEntity);
        return roleMapper.insert(roleEntity);
    }

    @Override
    public List<RoleDto> getRoleByParam(String searchParam) {
        Wrapper<RoleEntity> wrapper = new EntityWrapper<RoleEntity>();
        if (!StringUtils.isBlank(searchParam)) {
            wrapper.like("name", searchParam).or().like("code", searchParam);
        }
        List<RoleEntity> roleEntities = roleMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(roleEntities)) {
            return null;
        }
        Page<RoleDto> roleDtos = new Page<RoleDto>();
        for (RoleEntity roleEntity : roleEntities) {
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(roleEntity, roleDto);
            roleDtos.add(roleDto);
        }
        return roleDtos;
    }

    @Override
    public RoleDto getRoleById(Long id) {
        RoleEntity roleEntity = roleMapper.selectById(id);
        if (roleEntity == null) {
            return null;
        }
        RoleDto roleDto = new RoleDto();
        BeanUtils.copyProperties(roleEntity, roleDto);
        return roleDto;
    }

    @Override
    public Integer update(RoleDto roleDto) {
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(roleDto, roleEntity);
        Wrapper<RoleEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("id", roleDto.getId());
        return roleMapper.update(roleEntity, wrapper);
    }

    @Override
    public List<RoleDto> list() {
        List<RoleEntity> roleEntities = roleMapper.selectList(new EntityWrapper<>());
        if (CollectionUtils.isEmpty(roleEntities)) {
            return null;
        }
        List<RoleDto> roleDtos = new ArrayList<>(roleEntities.size());
        for (RoleEntity roleEntity : roleEntities) {
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(roleEntity, roleDto);
            roleDtos.add(roleDto);
        }
        return roleDtos;
    }

    private Page<RoleDto> toDto(Page<RoleEntity> roleEntities, Page<RoleDto> roleDtos) {
        BeanUtils.copyProperties(roleEntities, roleDtos);
        return roleDtos;
    }
}
